package com.wskj.web.controller.system;

import java.util.List;
import java.util.Arrays;

import com.wskj.system.bo.SysShareAddBo;
import com.wskj.system.bo.SysShareEditBo;
import com.wskj.system.bo.SysShareQueryBo;
import com.wskj.system.service.ISysShareService;
import com.wskj.system.vo.web.SysShareVo;
import lombok.AllArgsConstructor;

import javax.validation.constraints.*;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;
import com.wskj.common.annotation.RepeatSubmit;
import com.wskj.common.annotation.Log;
import com.wskj.common.core.controller.BaseController;
import com.wskj.common.core.domain.AjaxResult;
import com.wskj.common.enums.BusinessType;
import com.wskj.common.exception.CustomException;
import com.wskj.common.utils.poi.ExcelUtil;
import com.wskj.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

/**
 * 分享Controller
 *
 * @author wsdz
 * @date 2023-02-24
 */
@Api(value = "分享控制器", tags = {"分享管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/system/share")
public class SysShareController extends BaseController {

    private final ISysShareService iSysShareService;

    /**
     * 查询分享列表
     */
    @ApiOperation("查询分享列表")
    @PreAuthorize("@ss.hasPermi('system:share:list')")
    @GetMapping("/list")
    public TableDataInfo<SysShareVo> list(@Validated SysShareQueryBo bo) {
        return iSysShareService.queryPageList(bo);
    }

    /**
     * 导出分享列表
     */
    @ApiOperation("导出分享列表")
    @PreAuthorize("@ss.hasPermi('system:share:export')")
    @Log(title = "分享", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<Void> export(@Validated SysShareQueryBo bo) {
        List<SysShareVo> list = iSysShareService.queryList(bo);
        ExcelUtil<SysShareVo> util = new ExcelUtil<>(SysShareVo.class);
        return util.exportExcel(list, "分享");
    }

    /**
     * 获取分享详细信息
     */
    @ApiOperation("获取分享详细信息")
    @PreAuthorize("@ss.hasPermi('system:share:query')")
    @GetMapping("/{shareId}")
    public AjaxResult<SysShareVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("shareId") Long shareId) {
        return AjaxResult.success(iSysShareService.queryById(shareId));
    }

    /**
     * 新增分享
     */
    @ApiOperation("新增分享")
    @PreAuthorize("@ss.hasPermi('system:share:add')")
    @Log(title = "分享", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody SysShareAddBo bo) {
        return toAjax(iSysShareService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改分享
     */
    @ApiOperation("修改分享")
    @PreAuthorize("@ss.hasPermi('system:share:edit')")
    @Log(title = "分享", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody SysShareEditBo bo) {
        return toAjax(iSysShareService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除分享
     */
    @ApiOperation("删除分享")
    @PreAuthorize("@ss.hasPermi('system:share:remove')")
    @Log(title = "分享" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{shareIds}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] shareIds) {
        return toAjax(iSysShareService.deleteWithValidByIds(Arrays.asList(shareIds), true) ? 1 : 0);
    }

    /**
     * 导入分享数据
     */
    @ApiOperation("导入分享数据")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file")
    @PreAuthorize("@ss.hasPermi('system:share:import')")
    @Log(title = "分享" , businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult<Void> importData(MultipartFile file) {

        ExcelUtil<SysShareVo> util = new ExcelUtil<>(SysShareVo.class);
        List<SysShareVo> sysShareList;
        try {
            sysShareList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("文件解析异常！message：" + e.getMessage());
        }
        String message = iSysShareService.importSysShare(sysShareList);
        return AjaxResult.success(message);

    }

    /**
     * 下载模板
     *
     * @return AjaxResult 模板文件
     */
    @ApiOperation("下载模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<SysShareVo> util = new ExcelUtil<>(SysShareVo.class);
        return util.importTemplateExcel("分享数据");
    }
}
